package spacewar2.gui;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.ArrayList;

import javax.swing.JComponent;

import spacewar2.shadows.Shadow;
import spacewar2.utilities.Position;

/**
 * Main spacewar window component.  Note some of this code is from the original
 * spacewar 1 gui.
 * 
 * @author amy
 */
@SuppressWarnings("serial")
public class JSpacewarComponent extends JComponent {
    public static final Color TEXT_COLOR = new Color(0, 218, 159);
    public static final Font FONT12 = new Font("Arial", Font.BOLD, 12);
    public static final Font FONT10 = new Font("Arial", Font.BOLD, 10);
    
    public static final BasicStroke THIN_STROKE = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
    public static final BasicStroke STROKE = new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
    public static final BasicStroke THICK_STROKE = new BasicStroke(7, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);

	int height, width;
	
	ArrayList<Shadow> allShadows;
	

	public JSpacewarComponent(int height, int width) {
		super();
		this.height = height;
		this.width = width;

		setBackground(Color.GREEN);
        setMaximumSize(new Dimension(width, height));
        setPreferredSize(new Dimension(width, height));
        
        allShadows = new ArrayList<Shadow>();
	}

	public void addShadow(Shadow shadow) {
		allShadows.add(shadow);
	}
	
	public void removeShadow(Shadow shadow) {
		allShadows.remove(shadow);
	}
	
	
	/**
	 * Draw the space background and all the sub components
	 */
	protected void paintComponent(final Graphics g) {
		super.paintComponent(g);

        final Graphics2D graphics = (Graphics2D) g;
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        		RenderingHints.VALUE_ANTIALIAS_ON);

        // draw space
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, getWidth(), getHeight());
        
        ArrayList<Shadow> clonedShadows = new ArrayList<Shadow>(allShadows);
        // draw all the ships, beacons, etc
        for (Shadow shadow : clonedShadows) {
        	if (shadow.isDrawable()) {
        		drawShadow(shadow, graphics);
        	}
        }
        
	}
	
	/**
	 * Handles drawing things in a tororodially wrapped world.  Code
	 * comes from the spacewar 1 simulator.
	 * 
	 * @param shadow
	 * @param graphics
	 */
    private void drawShadow(final Shadow shadow, final Graphics2D graphics) {
        Position position = shadow.getActualLocation();
        double x = position.getX();
        double y = position.getY();
        if (x < shadow.getHalfWidth()) {
            //goes off screen to left
            shadow.setDrawLocation(new Position(x + width, y));
            shadow.draw(graphics);
            if (y < shadow.getHalfHeight()) {
                //also goes off screen to the bottom
                shadow.setDrawLocation(new Position(x, y + height));
                shadow.draw(graphics);
                shadow.setDrawLocation(new Position(x + width, y + height));
                shadow.draw(graphics);
            } else if (y >= height - shadow.getHalfHeight()) {
                //also goes off screen to the top
                shadow.setDrawLocation(new Position(x, y - height));
                shadow.draw(graphics);
                shadow.setDrawLocation(new Position(x + width, y - height));
                shadow.draw(graphics);
            }
        } else if (x >= width - shadow.getHalfWidth()) {
            //goes off screen to right
            shadow.setDrawLocation(new Position(x - width, y));
            shadow.draw(graphics);
            if (y < shadow.getHalfHeight()) {
                //goes off screen to bottom
                shadow.setDrawLocation(new Position(x, y + height));
                shadow.draw(graphics);
                shadow.setDrawLocation(new Position(x - width, y + height));
                shadow.draw(graphics);
            } else if (y >= height - shadow.getHalfHeight()) {
                //also goes off screen to the top
                shadow.setDrawLocation(new Position(x, y - height));
                shadow.draw(graphics);
                shadow.setDrawLocation(new Position(x - width, y - height));
                shadow.draw(graphics);
            }
        } else if (y < shadow.getHalfHeight()) {
            //goes off screen to bottom
            shadow.setDrawLocation(new Position(x, y + height));
            shadow.draw(graphics);
        } else if (y >= height - shadow.getHalfHeight()) {
            //goes off screen to top
            shadow.setDrawLocation(new Position(x, y - height));
            shadow.draw(graphics);
        }

        shadow.setDrawLocation(position);
        shadow.draw(graphics);
    }

	
}
